package com.caffedev.ottollotto;

import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class OttollottoWin extends ListActivity {

	private static final String TAG = "OttollottoWin";
	public static final int ID=0;
	public static final int INDEX_GRWNO=1;
	public static final int INDEX_GRWDATE=2;
	public static final int INDEX_FIRSTBALL=3;
	public static final int INDEX_SECONDBALL=4;
	public static final int INDEX_THIRDBALL=5;
	public static final int INDEX_FOURTHBALL=6;
	public static final int INDEX_FIFTHBALL=7;
	public static final int INDEX_SIXTHBALL=8;
	public static final int INDEX_BONUSBALL=9;
	
	private TextView txtgrwno;
	private ImageView b1;
	private ImageView b2;
	private ImageView b3;
	private ImageView b4;
	private ImageView b5;
	private ImageView b6;
	private ImageView b7;//bounus ball
	
	private ImageView mb1;
	private ImageView mb2;
	private ImageView mb3;
	private ImageView mb4;
	private ImageView mb5;
	private ImageView mb6;
	
	private ImageView grade;
	
	private static final String TABLE_NAME = "mTable";
	private Cursor wCursor;

	private int result=0; // don't know the grade
private String grwno;
private String[] nums;

//private ArrayAdapter<String> wAdapter;
private ImageAdapter wAdapter;
private List<String> lines;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		
		

		Intent intent = getIntent();
		grwno = intent.getStringExtra("grwno");
		nums = intent.getStringArrayExtra("number");
		
		
		String query = "select * from "+TABLE_NAME+" where grwno="+grwno;
	    wCursor = OttollottoMain.mDb.rawQuery(query, null);
        wCursor.moveToFirst();
        //Log.e(TAG,"cursor : "+wCursor.getCount());
        
		
		if(wCursor.getCount()< 1){
			setContentView(R.layout.yet);
			
			
		}else{
			setContentView(R.layout.win);
			
			
			result = areYouWin();
			
			
			
			txtgrwno = (TextView)findViewById(R.id.grwno);
			txtgrwno.setText(grwno + getString(R.string.round));
			
			b1 = (ImageView)findViewById(R.id.imageView1);
			b2 = (ImageView)findViewById(R.id.imageView2);
			b3 = (ImageView)findViewById(R.id.imageView3);
			b4 = (ImageView)findViewById(R.id.imageView4);
			b5 = (ImageView)findViewById(R.id.imageView5);
			b6 = (ImageView)findViewById(R.id.imageView6);
			b7 = (ImageView)findViewById(R.id.imageView7);
			
			b1.setBackgroundResource(getResources().getIdentifier("b" +Integer.valueOf(wCursor.getString(INDEX_FIRSTBALL)), "drawable", getPackageName()));
			b2.setBackgroundResource(getResources().getIdentifier("b" +Integer.valueOf(wCursor.getString(INDEX_SECONDBALL)), "drawable", getPackageName()));
			b3.setBackgroundResource(getResources().getIdentifier("b" +Integer.valueOf(wCursor.getString(INDEX_THIRDBALL)), "drawable", getPackageName()));
			b4.setBackgroundResource(getResources().getIdentifier("b" +Integer.valueOf(wCursor.getString(INDEX_FOURTHBALL)), "drawable", getPackageName()));
			b5.setBackgroundResource(getResources().getIdentifier("b" +Integer.valueOf(wCursor.getString(INDEX_FIFTHBALL)), "drawable", getPackageName()));
			b6.setBackgroundResource(getResources().getIdentifier("b" +Integer.valueOf(wCursor.getString(INDEX_SIXTHBALL)), "drawable", getPackageName()));
			b7.setBackgroundResource(getResources().getIdentifier("b" +Integer.valueOf(wCursor.getString(INDEX_BONUSBALL)), "drawable", getPackageName()));
			
			grade = (ImageView)findViewById(R.id.imageView15);
			grade.setBackgroundResource(getResources().getIdentifier("r"+result, "drawable", getPackageName()));
		}
		
		
		mb1 = (ImageView)findViewById(R.id.imageView8);
		mb2 = (ImageView)findViewById(R.id.imageView9);
		mb3 = (ImageView)findViewById(R.id.imageView10);
		mb4 = (ImageView)findViewById(R.id.imageView11);
		mb5 = (ImageView)findViewById(R.id.imageView12);
		mb6 = (ImageView)findViewById(R.id.imageView13);
		
		mb1.setBackgroundResource(getResources().getIdentifier("b"+ Integer.valueOf(nums[0]), "drawable", getPackageName()));
		mb2.setBackgroundResource(getResources().getIdentifier("b"+ Integer.valueOf(nums[1]), "drawable", getPackageName()));
		mb3.setBackgroundResource(getResources().getIdentifier("b"+ Integer.valueOf(nums[2]), "drawable", getPackageName()));
		mb4.setBackgroundResource(getResources().getIdentifier("b"+ Integer.valueOf(nums[3]), "drawable", getPackageName()));
		mb5.setBackgroundResource(getResources().getIdentifier("b"+ Integer.valueOf(nums[4]), "drawable", getPackageName()));
		mb6.setBackgroundResource(getResources().getIdentifier("b"+ Integer.valueOf(nums[5]), "drawable", getPackageName()));
		
		
		
		//compare nums with all luck number;
		
		
		wCursor = OttollottoMain.mCursor;
		wCursor.moveToFirst();
		//Log.e(TAG,"wCursor First : "+wCursor.getPosition());
		lines = new ArrayList<String>();
		
		while(!wCursor.isAfterLast()){
			
			int res = areYouWin();
			//Log.e(TAG,"wCursor  : "+wCursor.getPosition());
			if(res != 6){
				
				lines.add(wCursor.getString(INDEX_GRWNO) + " "+
						wCursor.getString(INDEX_FIRSTBALL)+ " "+
						wCursor.getString(INDEX_SECONDBALL)+ " "+
						wCursor.getString(INDEX_THIRDBALL)+ " "+
						wCursor.getString(INDEX_FOURTHBALL)+ " "+
						wCursor.getString(INDEX_FIFTHBALL)+ " "+
						wCursor.getString(INDEX_SIXTHBALL)+ " "+
						wCursor.getString(INDEX_BONUSBALL)+ " "+res);
						
			}
			wCursor.moveToNext();
		}
		
		//wAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,  lines);
		wAdapter = new ImageAdapter(this);
		//wAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		setListAdapter(wAdapter);
		
		
	}
	
	private int areYouWin(){

		int match_count=0;
		int grade = 6;//default 6 to bad luck
		
		 
		for(int i=0 ; i<=5 ; i++){

			for(int j = INDEX_FIRSTBALL ; j <= INDEX_BONUSBALL ; j++ ){
				if( Integer.valueOf(nums[i]) == Integer.valueOf(wCursor.getString(j)) ){
					match_count+=1;
				}
					
			}
		}
		
		switch(match_count){
		case 6: //1 or 2
			for(int i = 0; i<=5; i++){
				if(Integer.valueOf(nums[i]) == Integer.valueOf(wCursor.getString(INDEX_BONUSBALL))) // if bonus ball matched then grade 2
					grade = 2;
				else
					grade = 1;
			}
			break;
		case 5: //3
			grade = 3;
			break;
		case 4: //4
			grade = 4;
			break;
		case 3: //5
			grade = 5;
			break;
		}

		
		//OttollottoMain.mCursor.moveToPosition(mCursorPosition);
		return grade;
		/*
		 * 0 : i don't know
		 * 1~5 : win
		 * 6 : no win
		 */
	}
	@Override
	public void onBackPressed() {
		Intent intent = new Intent();
		intent.putExtra("grade", result );
		intent.putExtra("grwno", grwno );
		setResult(RESULT_OK, intent);
		finish();
		super.onBackPressed();
	}

	

////////////custom imageadapter
public class ImageAdapter extends BaseAdapter{
private Context mContext;

public ImageAdapter(Context c){
mContext = c;
}
public int getCount() {
return lines.size();
}
public Object getItem(int position) {
return lines.get(position);
}

public long getItemId(int position) {
return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
	
	
	
if(convertView == null){

	LayoutInflater inflater = LayoutInflater.from(mContext);
	convertView = (View)inflater.inflate(R.layout.winrow, parent,false);
}
	TextView grwno = (TextView)convertView.findViewById(R.id.grwno);
	ImageView ball1 = (ImageView)convertView.findViewById(R.id.image_ball1);
	ImageView ball2 = (ImageView)convertView.findViewById(R.id.image_ball2);
	ImageView ball3 = (ImageView)convertView.findViewById(R.id.image_ball3);
	ImageView ball4 = (ImageView)convertView.findViewById(R.id.image_ball4);
	ImageView ball5 = (ImageView)convertView.findViewById(R.id.image_ball5);
	ImageView ball6 = (ImageView)convertView.findViewById(R.id.image_ball6);
	ImageView bonus_ball = (ImageView)convertView.findViewById(R.id.image_bonus_ball);
	ImageView grade = (ImageView)convertView.findViewById(R.id.image_grade);
	
	String[] temp = lines.get(position).split(" ");
	
	grwno.setText(temp[0]);
	ball1.setBackgroundResource(getResources().getIdentifier("b" +temp[1], "drawable", getPackageName()));
	ball2.setBackgroundResource(getResources().getIdentifier("b" +temp[2], "drawable", getPackageName()));
	ball3.setBackgroundResource(getResources().getIdentifier("b" +temp[3], "drawable", getPackageName()));
	ball4.setBackgroundResource(getResources().getIdentifier("b" +temp[4], "drawable", getPackageName()));
	ball5.setBackgroundResource(getResources().getIdentifier("b" +temp[5], "drawable", getPackageName()));
	ball6.setBackgroundResource(getResources().getIdentifier("b" +temp[6], "drawable", getPackageName()));
	bonus_ball.setBackgroundResource(getResources().getIdentifier("b" +temp[7], "drawable", getPackageName()));
	grade.setBackgroundResource(getResources().getIdentifier("r" +temp[8], "drawable", getPackageName()));



	return convertView;
}

}
}

